#!/bin/zsh

set -o errexit

progname=$(basename $0)
usage() {
    echo >&2 "usage: $progname start VERSION"
    echo >&2 "       $progname finish VERSION"
    exit 1
}

if [[ $# != 2 ]]; then
    usage
fi

# Check that there is a line 'VERSION = "..."' in the wscript file.
grep >/dev/null '^VERSION = ".*"$' wscript
ACTION=$1
VERSION=$2

if [[ $1 = start ]]; then
    if [[ $(git rev-parse HEAD) != $(git rev-parse develop) ]]; then
        echo >&2 "must be on develop"
        exit 1
    fi
elif [[ $1 = finish ]]; then
    if [[ $(git rev-parse HEAD) != $(git rev-parse release/$VERSION) ]]; then
        echo >&2 "must be on release/$VERSION"
        exit 1
    fi
else
    usage
fi

update-wscript() {
    sed -i '' 's/^VERSION = "\(.*\)"$/VERSION = "'$1'"/' wscript
}

update-readme() {
    sed -i '' 's/\(Antares[- ]\)\([0-9.]*\)[0-9]/\1'$1'/g' README.rst
}

if [[ $ACTION == start ]]; then
    set -o verbose
    git checkout -b release/$VERSION
    update-wscript ${VERSION}rc1
    git commit wscript README.rst -m "Create release branch for $VERSION."
else
    set -o verbose
    update-wscript $VERSION
    update-readme $VERSION
    git commit wscript README.rst -m "Release Antares $VERSION."
    git checkout master
    git merge --no-ff release/$VERSION
    git tag v$VERSION
    git checkout develop
    git merge --no-ff release/$VERSION
fi
